import os
from library.configuration import mnemosyne

AddOption('--build-example',
           action="store", dest='selected_example',
           type='string',
           metavar='NAME',
           help='build this example')
AddOption('--build-bench',
           action="store", dest='selected_bench',
           type='string',
           metavar='NAME',
           help='build this example')
AddOption("--build-stats",
           action="store_true", dest="build_stats", 
           default = False,
           help="build library with profiling support")
AddOption('--config-name',
           action="store", dest='config_name',
           type='string',
           metavar='NAME',
           help='build using this configuration')
AddOption("--test",
           action="store_true", dest="run_tests", 
           default = False,
           help="run tests")
AddOption('--test-filter',
           action="store", dest='test_filter',
           type='string',
           metavar='NAME',
           help='a regular expression used to match tests to run')
AddOption('--config-ftrace',
           action="store_true", dest='config_ftrace',
           default = False,
           help='Compile trace infrastructure for NVM accesses. \
 Compiling the tracer does NOT initiate tracing. A runtime parameter in each of the shipped \
 benchmarks controls whether tracing actually takes place during execution. Compiling the tracer won\'t slow down your execution. \
 Initiating the tracer will.  \
 [DEFAULT : %default]')
AddOption('--verbose',
           action="store_true", dest='verbose',
           default = False,
           help='Set verbosity of compilation messages \
           [DEFAULT : %default]')



# Custom help message. Overrides the help output of the local options.
Help("""
Type: 'scons' to build the libraries.
      'scons check' to build and run unit tests.
""")

# Ugly hack to extract the optparse help output for my local options and add 
# it to our custom help text.
import SCons.Script.Main as main
help_lines = main.OptionsParser.format_help().split('\n')
count = 0
for help_line in help_lines:
	count = count+1
	if help_line == "Local Options:":
		break

option_help = '\n'.join(help_lines[count-1:])
Help(option_help)

mainEnv = mnemosyne.Environment(None)
mainEnv['BUILD_LINKAGE'] = "dynamic"
mainEnv['BUILD_DEBUG'] = True
mainEnv['BUILD_STATS'] = GetOption('build_stats')
mainEnv['BUILD_EXAMPLE'] = GetOption('selected_example')
mainEnv['BUILD_BENCH'] = GetOption('selected_bench')
mainEnv['BUILD_CONFIG_NAME'] = GetOption('config_name')
if mainEnv['BUILD_CONFIG_NAME'] is None:
	mainEnv['BUILD_CONFIG_NAME'] = 'default'
mainEnv['TEST_FILTER'] = GetOption('test_filter')
mainEnv['ENABLE_FTRACE'] = GetOption('config_ftrace') 
mainEnv['VERBOSE'] = GetOption('verbose')
mainEnv.set_verbosity()

rootDir = os.getcwd()
mainEnv['MY_ROOT_DIR'] = rootDir
mainEnv['MY_LINKER_DIR'] = os.path.join(rootDir, 'tool/linker')


mainEnv['CC'] = 'gcc'
mainEnv['CXX'] = 'g++'

Export('mainEnv')

CommonObjects = SConscript('library/common/SConscript', variant_dir = 'build/library/common')
pmallocLibrary = SConscript('library/pmalloc/SConscript', 'CommonObjects', variant_dir = 'build/library/pmalloc')
mcoreLibrary = SConscript('library/mcore/SConscript', 'CommonObjects', variant_dir = 'build/library/mcore')
mtmLibrary = SConscript('library/mtm/SConscript', variant_dir = 'build/library/mtm')

if mainEnv['BUILD_EXAMPLE'] != None:
	examplesEnv = mainEnv.Clone()
	examplesEnv['CPPPATH'] = ['#library/mcore/include', '#library/mtm/include']
	Export('examplesEnv')
	Export('mcoreLibrary', 'pmallocLibrary', 'mtmLibrary')
	SConscript('examples/SConscript', variant_dir = os.path.join('build', 'examples'))

if mainEnv['BUILD_BENCH'] != None:
	benchEnv = mainEnv.Clone()
	benchEnv['CPPPATH'] = ['#library/mcore/include', '#library/mtm/include']
	Export('benchEnv')
	Export('mcoreLibrary', 'pmallocLibrary', 'mtmLibrary')
	SConscript('bench/SConscript', variant_dir = os.path.join('build', 'bench'))

if GetOption('run_tests') == True:
	if mainEnv['TEST_FILTER'] is None:
		mainEnv['TEST_FILTER'] = ".*"
	Export('mcoreLibrary', 'pmallocLibrary', 'mtmLibrary')
	SConscript('test/SConscript', variant_dir = os.path.join('build', 'test'))
